package com.kaizzi.data.common.utils;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.*;

import java.io.IOException;
import java.lang.reflect.Type;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * @author Kzen
 * @Description jackson Json 工具类
 * @date 2020/9/8
 */
public class JsonUtil {

    public static final ObjectMapper mapper = new ObjectMapper();
    public static final JsonFactory jsonFactory;

    static {
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
        mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        dateFormat.setLenient(false);
        mapper.setDateFormat(dateFormat);
        jsonFactory = new JsonFactory();
    }

    public JsonUtil() {
    }

    public static ObjectMapper getObjectMapper() {
        return mapper;
    }

    public static final String serialize(Object o) {
        try {
            return o instanceof List ? mapper.writeValueAsString(((List)o).toArray()) : mapper.writeValueAsString(o);
        } catch (IOException var2) {
            throw new RuntimeException(var2.getMessage(), var2);
        }
    }

    public static <T> T deserialize(String json, Class<T> valueType) {
        try {
            return mapper.readValue(json, valueType);
        } catch (IOException var3) {
            throw new RuntimeException(var3.getMessage(), var3);
        }
    }

    public static <T> T deserialize(String json, final Type type) {
        try {
            return mapper.readValue(json, new TypeReference<T>() {
                public Type getType() {
                    return type;
                }
            });
        } catch (IOException var3) {
            throw new RuntimeException(var3.getMessage(), var3);
        }
    }

    public static <T> T deserialize(String json, Class<?> parentClass, Class... elementClasses) {
        try {
            JavaType javaType = mapper.getTypeFactory().constructParametrizedType(parentClass, parentClass, elementClasses);
            return mapper.readValue(json, javaType);
        } catch (IOException var4) {
            throw new RuntimeException(var4.getMessage(), var4);
        }
    }

    public static <T> T deserialize(String json, TypeReference<T> valueTypeRef) {
        try {
            return mapper.readValue(json, valueTypeRef);
        } catch (IOException var3) {
            throw new RuntimeException(var3.getMessage(), var3);
        }
    }

    public static JsonNode deserialize(String json) {
        try {
            return mapper.readTree(json);
        } catch (IOException var2) {
            throw new RuntimeException(var2.getMessage(), var2);
        }
    }

    public static ObjectMapper create(boolean isNoDefault) {
        return create(isNoDefault, "yyyy-MM-dd HH:mm:ss");
    }

    public static ObjectMapper create(boolean isNoDefault, String dateFormat) {
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(MapperFeature.PROPAGATE_TRANSIENT_MARKER, true);
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, true);
        if (isNoDefault) {
            mapper.setSerializationInclusion(JsonInclude.Include.NON_DEFAULT);
        }

        mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(dateFormat);
        simpleDateFormat.setLenient(false);
        mapper.setDateFormat(simpleDateFormat);
        return mapper;
    }

    public static void main(String[] args) {
        String serialize = JsonUtil.serialize("");
    }
}
